#include <iostream>
#include "Players.h"
#include "Command.h"
#include "Card.h"

// This function is called by playerturn print the ranks of cards on the table.
void printlistrank(list<Card> l)
{
	list<Card>::iterator i;
        if(l.empty()) {
                cout << endl;
                return;
        }
        for(i = l.begin(); i != l.end(); i++)
	{
		cout << " ";
                Rank r = i->rank_;
		switch(r)
		{
			case ACE:
				cout << 'A'; break;
			case TWO:
				cout << '2'; break;
			case THREE:
				cout << '3'; break;
			case FOUR:
				cout << '4'; break;
			case FIVE:
				cout << '5'; break;
			case SIX:
				cout << '6'; break;
			case SEVEN:
				cout << '7'; break;
			case EIGHT:
				cout << '8'; break;
			case NINE:
				cout << '9'; break;
			case TEN:
				cout << 10; break;
			case JACK:
				cout << 'J'; break;
			case QUEEN:
				cout << 'Q'; break;
			case KING:
				cout << 'K'; break;
                        case RANK_COUNT:
                                cout << ""; break;
		}
	}
	cout << endl;
}

// This is a simple helper function called by playerturn to print a list of Card.
void printlist(list<Card> l)
{
	list<Card>::iterator i;
        for(i = l.begin(); i != l.end(); i++)
	{
		if(!(i == l.begin())) cout << " ";
                cout << *i;
	}
	cout << endl;
}
// This function is called by playerturn if the user enters the command 'deck'. It prints the deck.
void printdeck(Card *allcard)
{
	for(int i = 0; i < 52; i++)
	{
		cout << allcard[i];
		if(i == 12 || i == 25 || i == 38 || i == 51)
		{
			cout << endl;
		}
		else
		{
			cout << " ";
		}
	}
        cout << ">";
}
// This is the function in the hPlayer class which is called by the Logic class.
// It handles all output and input for a human player's turn.
// It returns the command the player chooses.
Command hPlayer::playerturn(list<Card> playedClubs, list<Card> playedDiamonds, list<Card> playedHearts, list<Card> playedSpades, list<Card> legalmoves, Card *allcards)
{
	cout << "Cards on the table:\nClubs:";
	printlistrank(playedClubs);
	cout << "Diamonds:";
	printlistrank(playedDiamonds);
	cout << "Hearts:";
	printlistrank(playedHearts);
	cout << "Spades:";
	printlistrank(playedSpades);
	cout << "Your hand: ";
	printlist(cArray); 
        cout << "Legal plays: ";
	printlist(legalmoves);
	for(int j = 0;;j++)
	{
		Command c;
		if(j == 0) cout << ">";
                cin >> c;
                if(c.type == PLAY)
		{
			list<Card>::iterator i;
		        for(i = legalmoves.begin(); i != legalmoves.end(); i++)
			{
				if(i->suit_ == c.card.suit_ && i->rank_ == c.card.rank_)
				{
					cout << "Player " << pNum << " plays " << c.card << "." << endl; 
					return c;
				}
			}
			cout << "This is not a legal play." << endl;
                        cout << ">";
		}
		else if(c.type == DISCARD)
		{
			if(legalmoves.empty())
			{
				cout << "Player " << pNum << " discards " << c.card << "." << endl; 
				return c;
			}
			else
			{
				cout << "You have a legal play. You may not discard." << endl;
			}
		}
		else if(c.type == DECK)
		{
			printdeck(allcards);
		}
		else if(c.type == QUIT)
		{
			return c;
		}
		else if(c.type == RAGEQUIT)
		{
			cout << "Player " << pNum << " ragequits. A computer will now take over." << endl; 
			return c;
		}
	}
}
// This is the function in the cPlayer class which is called by the Logic class.
// It handles all output for a computer player's turn, as well as choosing which card to play or discard.
// It returns the command chosen (either PLAY or DISCARD).
Command cPlayer::playerturn(list<Card> playedClubs, list<Card> playedDiamonds, list<Card> playedHearts, list<Card> playedSpades, list<Card> legalmoves, Card *allcards)
{
	Command c;
	if(legalmoves.empty())
	{
		c.type = DISCARD;
		c.card = *(cArray.begin());
		cout << "Player " << pNum << " discards " << c.card  << "." << endl;
	}
	else
	{
		c.type = PLAY;
		c.card = *(legalmoves.begin());
		cout << "Player " << pNum << " plays " << c.card << "." << endl;

	}
	return c;
}
